package com.qf.ayc.address.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.ayc.address.entity.Address;
import com.qf.ayc.address.service.IAddressService;
import com.qf.ayc.address.vo.AddrVo;
import com.qf.ayc.utils.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author BeautyFairy
 * @since 2024-03-02
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private IAddressService iAddressService;

    @GetMapping("/getaddress")
    public ResultVo getaddress(Integer customerid) {
        log.info("用户id为{}", customerid);
        QueryWrapper<Address> customer1 = new QueryWrapper<Address>().eq("customer", customerid);
        List<Address> list = iAddressService.list(customer1);
        log.info("得到的地址list{}", list);
        if (list.size() > 0) {
            log.info("地址列表{}", list);
            return ResultVo.ok("1", "拿到该用户地址成功", list);
        }
        return ResultVo.ok("-1", "拿到地址失败");
    }

    @PostMapping("/add")
    public ResultVo add(@RequestBody Address address) {
        Integer isDefault = address.getIsDefault();

        if(isDefault==1){
            Integer customer = address.getCustomer();
            QueryWrapper<Address> wrapper = new QueryWrapper<>();
            wrapper.eq("customer",customer);
            List<Address> list = iAddressService.list(wrapper);
            for (Address address1 : list) {
                address1.setIsDefault(0);
                iAddressService.updateById(address1);
            }
        }
        log.info("地址信息{}", address);
        boolean save = iAddressService.save(address);
        if (save) {
            return ResultVo.ok("1", "成功添加一条地址信息");
        } else {
            return ResultVo.ok("-1", "添加地址失败");
        }
    }

    @GetMapping("/getaaddr")
    public ResultVo getaaddr(Integer addrid) {
        log.info("地址id为{}", addrid);
        QueryWrapper<Address> addr = new QueryWrapper<Address>().eq("id", addrid);
        Address address = iAddressService.getOne(addr);
        if (address != null) {
            return ResultVo.ok("1", "拿到该用户地址成功", address);
        } else {
            return ResultVo.notok("-1", "没找到这个地址id对应的信息");
        }
    }
    @PostMapping("/edit")//修改后端有问题
    public ResultVo editaddr(@RequestBody Address address){
        Integer isDefault = address.getIsDefault();

        if(isDefault==1){
            Integer customer = address.getCustomer();
            QueryWrapper<Address> wrapper = new QueryWrapper<>();
            wrapper.eq("customer",customer);
            List<Address> list = iAddressService.list(wrapper);
            for (Address address1 : list) {
                address1.setIsDefault(0);
                iAddressService.updateById(address1);
            }
        }
        log.info("编辑地址的值{}",address);
        boolean b = iAddressService.updateById(address);
        if(b){
            return ResultVo.ok("1", "拿到该用户地址成功", address);
        }
        return ResultVo.ok("-1", "修改地址信息失败", address);
    }
    @GetMapping("/deladdr")
    public ResultVo deladdr(Integer addrid){
        QueryWrapper<Address> wrapper = new QueryWrapper<>();
        wrapper.eq("id",addrid);
        boolean remove = iAddressService.remove(wrapper);
        if(remove){
            return ResultVo.ok("1", "删除地址成功", remove);
        }
        return ResultVo.ok("-1", "删除地址失败", remove);
    }
}

